unit clsMaquina;

interface

uses Dm,dbtables,biblio;

type TMaquina = class(TObject)
  public
    Dep_codigo       : string;
    Descricao_maquina: string;
    Responsavel      : string;
    Custo_Hora       : double;
    procedure Gravar(P_cod_maquina:String);
    function  Recuperar(P_cod_maquina:String):Byte;
    function  Excluir(P_cod_maquina:String):Byte;
    function  Qtde_Maquinas(p_Dep_Codigo: string): integer;
    function  BuscaInicialFinal(IniFim:integer):string;
    procedure Listar_Maquinas(p_Dep_Codigo: string; var tq: TQuery);
    procedure Consultar(var p_q:TQuery; P_Descricao_maquina:String);
  end;
implementation

{ TMaquina }

function TMaquina.BuscaInicialFinal(IniFim: integer): string;
var
  tq: TQuery;
begin
  tq := TQuery.create(DmInt);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := false;
  tq.Params.clear;
  tq.sql.clear;
  tq.sql.add('Select * from maquina order by cod_maquina');
  tq.open;
  // IniFim  = 1-inicial   2-final
  if IniFim=2 then
    tq.Last;
  Result := tq.fieldbyname('cod_maquina').AsString;
  tq.close;
  tq.free;
end;

procedure TMaquina.Consultar(var p_q: TQuery; P_Descricao_maquina: String);
begin
  p_q.SQL.clear;
  p_q.Params.clear;
  p_q.sql.add('Select cod_maquina,descricao_maquina,dep_descricao from Maquina,departamento '+
       'where descricao_maquina>=:K_descricao_maquina and departamento.dep_codigo=maquina.dep_codigo order by descricao_maquina');
  p_q.ParamByName('K_descricao_maquina').AsString := P_Descricao_maquina;
end;

function TMaquina.Excluir(P_cod_maquina: String): Byte;
VAR
  tq :TQuery;
begin
  tq := TQuery.create(DmInt);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := false;
  tq.sql.clear;
  tq.Params.clear;
  tq.sql.add('Delete from maquina where cod_maquina=:K_cod_maquina');
  tq.ParamByName('K_cod_maquina').AsString := StrZero(P_cod_maquina,3);
  tq.ExecSQL;
  Result := 1;
  tq.Close;
  tq.free;
end;

procedure TMaquina.Gravar(P_cod_maquina: String);
var tq :TQuery;
begin
  tq := TQuery.create(DmInt.Db);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.active := false;
  tq.sql.clear;
  tq.Params.clear;
  tq.sql.add('Select * from maquina where cod_maquina=:K_cod_maquina');
  tq.ParamByName('K_cod_maquina').AsString := strzero(P_cod_maquina,3);
  tq.open;
  if tq.eof then
  begin
    tq.sql.clear;
    tq.Params.clear;
    tq.sql.add('insert into maquina values(:K_cod_maquina,'+
               ':K_dep_codigo       ,'+
               ':K_descricao_maquina,'+
               ':K_Responsavel      ,'+
               ':K_Custo_Hora       )');
  end
  else
  begin
    tq.sql.clear;
    tq.Params.clear;
    tq.sql.add('Update maquina set                     '+
               'dep_codigo       =:K_dep_codigo       ,'+
               'descricao_maquina=:K_descricao_maquina,'+
               'Responsavel      =:K_Responsavel      ,'+
               'Custo_Hora       =:K_Custo_Hora        '+
               'Where cod_maquina=:K_cod_maquina       ');
  end;
  tq.ParamByName('K_cod_maquina').AsString      := strzero(P_cod_maquina,3);
  tq.ParamByName('K_dep_codigo').AsString       := Dep_codigo;
  tq.ParamByName('K_descricao_maquina').AsString:= Descricao_maquina;
  tq.ParamByName('K_Responsavel').AsString      := Responsavel;
  tq.ParamByName('K_Custo_Hora').Asfloat        := Custo_Hora;
  tq.ExecSQL;
  tq.close;
  tq.free;
end;

procedure TMaquina.Listar_Maquinas;
begin
 tq.close;
 tq.sql.clear;
 tq.params.clear;
 tq.sql.add('Select * from Maquina where Dep_Codigo=:a');
 tq.params[0].asstring:= p_Dep_Codigo;
 tq.open;
end;

function TMaquina.Qtde_Maquinas;
var tq: TQuery;
begin
 tq             := TQuery.create(dmint);
 tq.DatabaseName:= dmint.db.DatabaseName;
 tq.Active      := false;
 tq.sql.clear;
 tq.params.clear;
 tq.sql.add('Select count(*) qtde from Maquina where Dep_Codigo=:a');
 tq.params[0].asstring:= p_Dep_Codigo;
 tq.open;
 result:= tq.fieldbyname('qtde').asinteger;
 tq.close;
 tq.free;
end;

function TMaquina.Recuperar(P_cod_maquina: String): Byte;
var
  tq: TQuery;
begin
  tq := TQuery.create(DmInt);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := false;
  tq.Params.clear;
  tq.sql.clear;
  tq.sql.add('Select * from maquina where cod_maquina=:K_cod_maquina');
  tq.ParamByName('K_cod_maquina').AsString := StrZero(P_cod_maquina,3);
  tq.open;
  if not tq.eof then
    Result := 1
  else
    Result := 0;
  Dep_codigo       := tq.fieldbyname('dep_codigo').AsString;
  Descricao_maquina:= tq.fieldbyname('descricao_maquina').AsString;
  Responsavel      := tq.fieldByName('Responsavel').AsString;
  Custo_Hora       := tq.fieldByName('Custo_Hora').Asfloat;
  tq.close;
  tq.free;
end;

end.
